package com.ashapo.example;

public class Example017Instanceof {

    /**
     * instanceof运算符用于测试指定对象是否是指定类型(类或子类或接口)的实例。
     * @param args
     */
    public static void main(String[] args) {
        Example017Instanceof s = new Example017Instanceof();
        System.out.println(s instanceof Example017Instanceof);// true

        Printable p = new BBB();
        Call c = new Call();
        c.invoke(p);
    }
}

interface Printable {
}

class AAA implements Printable {
    public void a() {
        System.out.println("a method");
    }
}

class BBB implements Printable {
    public void b() {
        System.out.println("b method");
    }
}

class Call {
    void invoke(Printable p) {// upcasting
        if (p instanceof AAA) {
            AAA a = (AAA) p;// Downcasting
            a.a();
        }
        if (p instanceof BBB) {
            BBB b = (BBB) p;// Downcasting
            b.b();
        }

    }
}
